package com.mz.ieann.aide.city.adapter

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import com.mz.ieann.aide.BR
import com.mz.ieann.aide.R
import com.mz.ieann.aide.base.vm.BaseVMAdapter
import com.mz.ieann.aide.city.bean.City
import com.mz.ieann.aide.databinding.AdapterCityNameBinding

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by Administrator
 * @date on 2021 2021/10/23 14:09
 */
class CityNameAdapter(private val nameList : ArrayList<City>, recyclerView : RecyclerView) :
    BaseVMAdapter<City, AdapterCityNameBinding>(nameList, R.layout.adapter_city_name, BR.city) {

    private val layoutManager = recyclerView.layoutManager as GridLayoutManager

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (nameList[holder.adapterPosition].isFirst) {
            holder.binding.nameLetter.visibility = View.VISIBLE
        } else {
            holder.binding.nameLetter.text = ""
            if (holder.adapterPosition / layoutManager.spanCount == 0) {
                holder.binding.nameLetter.visibility = View.VISIBLE
            } else {
                holder.binding.nameLetter.visibility = View.GONE
            }
        }
    }

    override fun onViewAttachedToWindow(holder: ViewHolder) {
        super.onViewAttachedToWindow(holder)
        layoutManager.spanSizeLookup = object : SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if ("#" == nameList[position].firstLetter){
                    return 1
                }
                return layoutManager.spanCount
            }
        }
    }

    fun getClickItemIndex(letter : String?) :Int{
        for (i in nameList.indices){
            if (letter == nameList[i].firstLetter){
                return i
            }
        }
        return -1
    }


}